package com.ckv.qcm.model;

import java.sql.SQLException;
import java.util.ArrayList;

import com.ckv.qcm.model.dao.DAO;
import com.ckv.qcm.model.dao.TestDAO;

public class Test {

	private long id;
	private String name;
	private int time;
	private float treshold;
	private static TestDAO dao = new TestDAO();
	private int nbQuestions = 0;
	
	private ArrayList<Section> sections = new ArrayList<Section>();

	private Trainer trainer;

	public Test() {
		super();
	}

	public Test(long id, String name, int time, float treshold) {
		super();
		this.id = id;
		this.name = name;
		this.time = time;
		this.treshold = treshold;
	}
	
	public static Test find(long id) throws SQLException
	{
		Test t = dao.find(id);
		t.loadNbQuestions();
		return t;
	}
	
	public void loadSections() throws SQLException
	{
		ArrayList<Long> sectionIds = dao.getSectionIds(this.id);
		this.nbQuestions = 0;
		
		for (Long sectionId : sectionIds)
		{
			Section sec = Section.find(sectionId);
			
			this.nbQuestions += sec.getNbQuestions();
			
			this.sections.add(sec);
		}
	}
	
	public void loadNbQuestions() throws SQLException
	{
		this.nbQuestions = dao.getNbQuestions(this.id);
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public float getTreshold() {
		return treshold;
	}

	public void setTreshold(float treshold) {
		this.treshold = treshold;
	}

	public ArrayList<Section> getSections() {
		return sections;
	}

	public void setSections(ArrayList<Section> sections) {
		this.sections = sections;
	}

	public Trainer getTrainer() {
		return trainer;
	}

	public void setTrainer(Trainer trainer) {
		this.trainer = trainer;
	}

	public static ArrayList<Long> findQuestionsBySession(Long idSession) throws SQLException
	{
		System.out.println("blabla");
		ArrayList<Long> ret= dao.findQuestionsBySession(idSession);
		for (Long id : ret) {
			System.out.println(id);
			
		}
		return ret;
	}
}
